package org.iteam.xlz.manager.action.sys;

import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.core.Constant;
import org.iteam.core.Constant.Function;
import org.iteam.core.Constant.Status;
import org.iteam.core.model.sys.SysFunction;
import org.iteam.core.service.sys.SysFunctionService;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 菜单管理
 */
@SuppressWarnings("unchecked")
@Results({
		@Result(type = "freemarker", name = "INDEX", location = "/WEB-INF/content/sys/module-index.html"),
		@Result(type = "freemarker", name = "EDIT", location = "/WEB-INF/content/sys/module-edit.html") })
public class SysModuleAction extends AbstractModuleAction<SysFunction> implements RestAction, ModelDriven<SysFunction> {
	
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(PATH + "/index")
	public String index(){
		map.put("status", Status.values());
		map.put("modes", Function.Mode.values());
		return INDEX;
	}

	/**
	 * 查询
	 * 
	 */
	@Action(PATH + "/query")
	public String query() {
		try {
			map.put("total", service.countByType(Function.Type.MODULE.ecode, _parent));
			map.put("rows", service.queryByPageType(page, rows, Function.Type.MODULE.ecode, _parent));
		} catch (Exception e) {
			e.printStackTrace();
			map.put("total", 0);
			map.put("rows", new ArrayList<SysFunction>());
		}
		return MAP;
	}

	/**
	 * 查看单条记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}")
	public String show() {
		return null;
	}

	/**
	 * 创建记录
	 * 
	 * @return
	 */
	@Action(PATH + "/create")
	public String create() {
		try {
			model.setType(Constant.Function.Type.MODULE.ecode);
			map.put("state", service.create(model));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 更新记录
	 * 
	 * @return
	 */
	@Action(PATH + "/update")
	public String update() {
		try {
			model.setType(Constant.Function.Type.MODULE.ecode);
			map.put("state", service.update(model));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 删除记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/delete")
	public String delete() {
		try {
			map.put("state", service.delete(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 销毁记录
	 * 
	 * @return
	 */
	@Action(PATH + "/{_id}/destroy")
	public String destroy() {
		try {
			map.put("state", service.destroy(_id));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MAP;
	}

	/**
	 * 编辑记录
	 * 
	 * @return
	 */

	@Action(PATH + "/{_id}/edit")
	public String edit(){
		try {
			object = service.show(_id);
			map.put("status", Status.values());
			map.put("menus", service.show(_parent));
			map.put("modes", Function.Mode.values());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return EDIT;
	}

	/**
	 * 新增记录
	 * 
	 * @return
	 */
	@Action(PATH + "/new")
	public String editNew(){
		try {
			object = model;
			map.put("status", Status.values());
			map.put("modes", Function.Mode.values());
			map.put("menus", service.show(_parent));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return EDIT;
	}

	public SysFunction getModel() {
		if (model == null)
			model = new SysFunction();
		return model;
	}

	private static final long serialVersionUID = -5800011834083416221L;
	private SysFunctionService service = SysFunctionService.getInstance();
	private final String PATH = "/sys/module/{_parent}";

}
